extends PanelContainer


@onready var tool_axe: Button = $MarginContainer/HBoxContainer/ToolAxe
@onready var tool_tilling: Button = $MarginContainer/HBoxContainer/ToolTilling
@onready var tool_watering_can: Button = $MarginContainer/HBoxContainer/ToolWateringCan
@onready var tool_corn: Button = $MarginContainer/HBoxContainer/ToolCorn
@onready var tool_tomato: Button = $MarginContainer/HBoxContainer/ToolTomato



func _ready():
	ToolManager.enable_tool.connect(on_enable_tool)

	set_tool_enabled(tool_tilling, false)
	set_tool_enabled(tool_watering_can, false)
	set_tool_enabled(tool_corn, false)
	set_tool_enabled(tool_tomato, false)


func set_tool_enabled(tool_button: Button, enable: bool):
	if enable:
		tool_button.disabled = false
		tool_button.focus_mode = Control.FOCUS_ALL
	else:
		tool_button.disabled = true
		tool_button.focus_mode = Control.FOCUS_NONE



func _on_tool_axe_pressed() -> void:
	ToolManager.select_tool(DataTypes.Tools.AxeWood)



func _on_tool_tilling_pressed() -> void:
	ToolManager.select_tool(DataTypes.Tools.TillGround)


func _on_tool_watering_can_pressed() -> void:
	ToolManager.select_tool(DataTypes.Tools.WaterCrops)


func _on_tool_corn_pressed() -> void:
	ToolManager.select_tool(DataTypes.Tools.PlantCorn)


func _on_tool_tomato_pressed() -> void:
	ToolManager.select_tool(DataTypes.Tools.PlantTomato)


func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("release_tool"):
		ToolManager.select_tool(DataTypes.Tools.None)
		tool_axe.release_focus()
		tool_tilling.release_focus()
		tool_watering_can.release_focus()
		tool_corn.release_focus()
		tool_tomato.release_focus()


func on_enable_tool(tool: DataTypes.Tools):
	if tool == DataTypes.Tools.TillGround:
		set_tool_enabled(tool_tilling, true)
	elif tool == DataTypes.Tools.WaterCrops:
		set_tool_enabled(tool_watering_can, true)
	elif tool == DataTypes.Tools.PlantCorn:
		set_tool_enabled(tool_corn, true)
	elif tool == DataTypes.Tools.PlantTomato:
		set_tool_enabled(tool_tomato, true)
